package leetcode.code557.ReverseWordsinaStringIII;

/**
 *
 * �м��ж���ո��ʱ�������⣬��֪���ܲ��ܹ���û��
 *
 * @author Administrator
 *
 */
public class Solution1 implements IReverseWordsinaStringIII {

	@Override
	public String reverseWords(String s) {
		if (s == null || s.length() == 0)
			return s;
		String[] arr = s.split(" ");
		StringBuffer sb = new StringBuffer();
		if (s.charAt(0) == ' ')
			sb.append(' ');
		for (String str : arr) {
			sb.append(reverseString(str.toCharArray()));
		}
		if (s.charAt(s.length() - 1) != ' ')
			return sb.toString().substring(0, sb.toString().length() - 1);
		return sb.toString();
	}

	public String reverseString(char[] s) {
		if (s == null || s.length == 0)
			return "";
		for (int i = 0, j = s.length - 1; i <= j; i++, j--) {
			char temp = s[i];
			s[i] = s[j];
			s[j] = temp;
		}
		return new String(s) + " ";
	}

	public static void main(String[] args) {
		IReverseWordsinaStringIII i = new Solution1();
		i.debug1(i);
	}

	@Override
	public String debug(String s, IReverseWordsinaStringIII i) {
		System.out.println(i.reverseWords(s));
		return null;
	}

	@Override
	public String debug1(IReverseWordsinaStringIII i) {
//		���룺"Let's take LeetCode contest"
//		�����"s'teL ekat edoCteeL tsetnoc"
		String s = " Let's take  LeetCode contest ";
		String r = " s'teL ekat  edoCteeL tsetnoc ";
		System.out.println(r);
		this.debug(s, i);
		return null;
	}

	@Override
	public String debug2(IReverseWordsinaStringIII i) {

		return null;
	}

}
